//
//  SettingViewController.m
//  FactorySale
//
//  Created by 主事丫环 on 2020/4/3.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingTableViewCell.h"
#import "ShippingAddressViewController.h"
#import "TuiChuLoginAlertView.h"
#import "LoginViewController.h"
#import "JPUSHService.h"

#import "CommonPopViewController.h"

@interface SettingViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic,strong) TuiChuLoginAlertView *alert;

@end

@implementation SettingViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = Main_BackGroundColor;
    
    _titleArray = @[@"收货地址", @"退出登录"];
    [self.view addSubview:self.myTableView];
    
    // Do any additional setup after loading the view.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //定义Cell标识 每一个Cell对应一个自己的标识
    NSString *CellIdentitier = [NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section, (long)indexPath.row];
    //通过唯一标识创建Cell实例
    SettingTableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:CellIdentitier];
    //判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,w而不会再次初始化)
    if (!mycell) {
        mycell = [[SettingTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentitier];
    }
    
    mycell.selectionStyle = UITableViewCellSelectionStyleNone;//取消cell的点击状态
    mycell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    mycell.cellTitleLab.text = self.titleArray[indexPath.row];
    
    return mycell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        //跳转到收货地址列表
        ShippingAddressViewController *vc = [[ShippingAddressViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        //退出登录
        [self tuiChuLoginBtnClick];
    }
}

-(void)tuiChuLoginBtnClick {
    kWeakSelf;
    CommonPopViewController *popVC = [CommonPopViewController new];
    popVC.message = @"确定要退出登录？";
    popVC.cancleBtnTitle = @"取消";
    popVC.confirmBtnTitle = @"确定";
    popVC.confirmBtnClick = ^{
        [weakSelf loginOut];
    };
    [self presentViewController:popVC animated:YES completion:nil];
}

-(void)loginOut{
    //退出登录
    [MBProgressHUD showHUDAddedTo:self.view  animated:YES];
    [NetWorkTool POST:factoryLoginOut param:nil
              success:^(id dic) {
        
        [USER_DEFAULT removeObjectForKey:@"Token"];
        [USER_DEFAULT removeObjectForKey:@"user_id"];
        ShowToastWithText(@"退出成功");
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        //登录界面
        LoginViewController *vc = [[LoginViewController alloc]init];
        BaseNavViewController *nav = [[BaseNavViewController alloc]initWithRootViewController:vc];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];
        [JPUSHService setAlias:@"" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
        } seq:1];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

-(UITableView *)myTableView{
    if (!_myTableView) {
        
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT) style:(UITableViewStyleGrouped)];
        //_myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        //_myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.tableFooterView = [UIView new];
        adjustInset(_myTableView);
        
        [_myTableView registerClass:[SettingTableViewCell class] forCellReuseIdentifier:@"SettingTableViewCell"];
        
        kWeakSelf;
        weakSelf.myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
            [weakSelf.myTableView.mj_header beginRefreshing];
        }];
        
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

@end
